package com.wxit.educenter.controller;

import com.wxit.commonutils.JwtUtils;
import com.wxit.commonutils.R;
import com.wxit.commonutils.ordervo.UcenterMemberOrder;
import com.wxit.educenter.entity.UcenterMember;
import com.wxit.educenter.entity.vo.RegisterVo;
import com.wxit.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 吴杰
 * @since 2021-12-14
 */
@RestController
@RequestMapping("/educenter/member")
//@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    /**
     * 登录
     * @param member
     * @return
     */
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member){
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }

    /**
     * 注册
     * @param registerVo
     * @return
     */
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    /**
     * 根据token获取用户的信息
     * @param request
     * @return
     */
    @GetMapping("getMemberInfo")
    public R getMemberInfoByToken(HttpServletRequest request){
        //调用jwt工具类的方法，根据request对象获取头信息，返回用户的id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库，根据用户的id，获取用户的信息
        UcenterMember member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

    //根据token字符串获取用户信息
    @GetMapping("getInfoUc/{id}")
    public UcenterMember getInfo(@PathVariable String id){
        UcenterMember ucenterMember = memberService.getById(id);
        UcenterMember member = new UcenterMember();
        BeanUtils.copyProperties(ucenterMember,member);
        return member;
    }

    //根据用户id获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
        UcenterMember member = memberService.getById(id);
        //把member对象里面的信息复制给封装的对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    //查询某一天注册人数
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
       Integer count =  memberService.countRegisterDay(day);
       return R.ok().data("countRegister",count);
    }
}

